import 'package:flutter/material.dart';
import 'dart:math' as math;

import 'package:get/get.dart';

class QuizSearchingPlayers extends StatefulWidget {
  const QuizSearchingPlayers({Key? key}) : super(key: key);

  @override
  _QuizSearchingPlayersState createState() => _QuizSearchingPlayersState();
}

class _QuizSearchingPlayersState extends State<QuizSearchingPlayers>
    with SingleTickerProviderStateMixin {
  late AnimationController _controller;
  int animationIndex = 0;

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(
      value: 0,
      duration: const Duration(milliseconds: 500),
      lowerBound: -10,
      upperBound: 10,
      vsync: this,
    )..repeat(reverse: true);
  }

  @override
  void dispose() {
    debugPrint('==============dispose====================');
    _controller.stop();
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      backgroundColor: Colors.red,
      extendBody: true,
      floatingActionButton: TextButton(
          onPressed: () {
            if (animationIndex >= 3) {
              animationIndex = 0;
            } else {
              animationIndex += 1;
            }
            setState(() {});
          },
          child: Text('切换')),
      body: Container(
        alignment: Alignment.center,
        color: Colors.orange,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: [
            _makeAnimationWidget(_makeWidget(), animationIndex == 0),
            _makeAnimationWidget(_makeWidget(), animationIndex == 1),
            _makeAnimationWidget(_makeWidget(), animationIndex == 2),
            _makeAnimationWidget(_makeWidget(), animationIndex == 3),
          ],
        ),
      ),
    );
  }

  Widget _makeAnimationWidget(Widget child, bool needAnimation) {
    if (!needAnimation) {
      return child;
    }
    return AnimatedBuilder(
      animation: _controller,
      child: child,
      builder: (context, child) {
        return Transform.translate(
          offset: Offset(0, _controller.value),
          child: child,
        );
      },
    );
  }

  Widget _makeWidget() {
    return Container(
      width: 100,
      height: 100,
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(50),
        color: Colors.blue,
      ),
    );
  }
}
